package model;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 
 * This is the Order class. This class contain the date the amount and the title
 * of the order. <br>
 * @author George <br>
 * @changes:
 */
public class Order {

	private String title;
	private GregorianCalendar date;
	// private Map<SparePart, Integer> parts = new HashMap<SparePart,
	// Integer>();
	private List<OrderLine> orderLines = new ArrayList<OrderLine>();

	/**
	 * <code>  Order order = new Order(title,date); </code>
	 * 
	 * @author George
	 * @param String
	 *            title = The spare part type.
	 */
	public Order(String title) {
		this.title = title;
		this.date = new GregorianCalendar();
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDate() {
		if (date != null) {
			String month = date.get(GregorianCalendar.DATE) + "";
			String day = date.get(GregorianCalendar.MONTH) + "";
			String year = date.get(GregorianCalendar.YEAR) + "";

			if (date.get(GregorianCalendar.DATE) < 10)
				month = "0" +date.get(GregorianCalendar.DATE);
			if ((date.get(GregorianCalendar.MONTH) + 1) < 10)
				day = "0" + (date.get(GregorianCalendar.MONTH) + 1);

			return day + "-" + month + "-" + year;
		} else
			return "N/A";
	}

	public void setDate(GregorianCalendar date) {
		this.date = date;
	}

	// public Map<spe, amount> getParts() {
	// return parts;
	// }

	// public void setParts(Map<SparePart, amount> parts) {
	// this.parts = parts;
	// }

	public List<OrderLine> getOrderLines() {
		return orderLines;
	}

	// ------------------------------------------------------//

	/**
	 * Adds a OrderLine to this Order Requires: OrderLine != null and OrderLine
	 * is not an any other Orders
	 */
	public void addOrderLine(OrderLine orderLine) {
		if (!orderLines.contains(orderLine)) {
			orderLines.add(orderLine);
		}
	}

	/**
	 * Getting the OrderLine list
	 */
	public List<OrderLine> returnOrderLine() {
		return orderLines;
	}

	/**
	 * Removes a OrderLine from this Order. Requires: OrderLine != null
	 */
	public void removeOrderLinet(OrderLine orderLine) {
		if (orderLines.contains(orderLine)) {
			orderLines.remove(orderLine);
		}
	}

	// -------------------------------------------------------------------------

	public void updateOrderLine(SparePart part, int amount, OrderLine orderline) {
		orderline.updateOrderLine(part, amount);
	}

	public void updateOrder(String title) {
		this.title = title;
	}

	// ------------------------------------------------------------------------

	/**
	 * This method is
	 */
	public void createOrderLine(int amount, SparePart part) {
		orderLines.add(new OrderLine(amount, part));
	}

	// ------------------------------------------------------//
	public String toString() {
		return title;
	}

	// ------------------------------------------------------//

	/**
	 * This is the OrderLine class. This class contain the amount of the spare
	 * part and the specific spare part type. <br>
	 * @author George <br>
	 * @changes:
	 */
	public class OrderLine {
		private int amount;
		private SparePart sparePart;

		/**
		 * <code>  OrderLine orderLine = new OrderLine(amount, sparePart); </code>
		 * 
		 * @author George
		 * @param int amount = The amount of the spare part. </br> SparePart
		 *        sparePar = The specific spare part type .
		 */
		private OrderLine(int amount, SparePart sparePart) {
			this.amount = amount;
			this.sparePart = sparePart;
		}

		public int getAmount() {
			return amount;
		}

		public void setAmount(int amount) {
			this.amount = amount;
		}

		public SparePart getSparePart() {
			return sparePart;
		}

		public void setSparePart(SparePart sparePart) {
			this.sparePart = sparePart;
		}

		public void updateOrderLine(SparePart part, int amount) {
			this.sparePart = part;
			this.amount = amount;
		}

		public String toString() {
			return "Part " + sparePart.getIDnumber() + " - "+ amount + " units";
		}
	}
	// ------------------------------------------------------//
}
